package com.tanhua.dubbo.api;


import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.ActionType;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }
    /**
     * * 查询指定用户发布的所有视频列表
     * */
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Video> videos = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pagesize, count, videos);
    }






    /**
     * * 查询用户是否已经关注
     *
     **/
    public boolean hasFollowed(Long userId, Long videoUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("followUserId").is(videoUserId));
        return mongoTemplate.exists(query, FocusUser.class);
    }
    /**
     * * 关注/取消关注
     **/
    public void followOrNot(FocusUser focusUser, ActionType actionType) {
        //1、判断是关注还是取消关注
        if (actionType.getType() == ActionType.LIKE.getType()) {
            //保存关注状态
            mongoTemplate.save(focusUser);
        } else {
            //删除关注状态
            Query query = Query.query(Criteria.where("userId").is(focusUser.getUserId()).and("followUserId").is(focusUser.getFollowUserId()));            mongoTemplate.remove(query, FocusUser.class);
        }
    }
    /**
     * * 更新视频点赞数量
     * */
    public void likeOrDislike(ObjectId videoId, ActionType actionType) {
        Query query = Query.query(Criteria.where("_id").is(videoId));
        Update update = new Update();
        //判断是点赞还是取消点赞
        if (actionType.getType() == ActionType.LIKE.getType()) {
            //点赞数量加一
            update.inc("likeCount", 1);
        } else {
            //点赞数量减一
            update.inc("likeCount", -1);
        }
        mongoTemplate.updateFirst(query, update, Video.class);
    }
    /**
     * 查询评论列表
     * */
    public List<Comment> queryComments(ObjectId videoId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(videoId).and("commentType").is(CommentType.COMMENT.getType())).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Comment.class);
    }
    /**
     * * 根据id查询
     * */
    public Video findById(ObjectId videoId) {
        return mongoTemplate.findById(videoId, Video.class);
    }
    /**
     *  保存视频评论
     *  */
    public void saveVideoComment(Comment comment) {
        //保存评论
        mongoTemplate.save(comment);
        //视频评论数加一
        Query query = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update().inc("commentCount", 1);
        mongoTemplate.updateFirst(query, update, Video.class);
    }
    /**
     * * 更新视频评论点赞数量
     **/
    public void likeOrDislikeComment(ObjectId commentId, ActionType actionType) {
        Query query = Query.query(Criteria.where("_id").is(commentId));
        Update update = new Update();
        //判断是点赞还是取消点赞
        if (actionType.getType() == ActionType.LIKE.getType()) {
            //点赞数量加一
            update.inc("likeCount", 1);
        } else {
            //点赞数量减一
            update.inc("likeCount", -1);
        }        mongoTemplate.updateFirst(query, update, Comment.class);
    }
}
